﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using ChuangDu_Spirit_QGB_BLL;
using ChuangDu_Spirit_QGB_IBLL;
namespace ChuangDu_Spirit_QGB_BLLContainer
{   /// <summary>
/// 该类是创建IBLL的实例
/// </summary>
    public class BLLContainer
    {   /// <summary>
    /// IOC容器
    /// </summary>
        public static IContainer container = null;
        public static IT Resolve<T,IT>()
        {
            try
            {
                if (container == null)
                {
                    Initialise<T,IT>();
                }
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("ioc实列化出错" + ex.Message);
            }
            return container.Resolve<IT>();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialise<T, IT>()
        {
            var builder = new ContainerBuilder();
            //格式：builder.RegisterType<xxxx>().As<Ixxxx>().InstancePerLifetimeScope();
            builder.RegisterType<T>().As<IT>().InstancePerLifetimeScope();
            container = builder.Build();

        }
    }
}
